<template>
  <div class="content">
    <el-form ref="formRef" label-width="100px" :model="form" :rules="rules">
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="基础信息" name="first">
          <!-- ! 第一行 -->
          <el-form-item label="标题" prop="title">
            <el-input v-model="form.title" placeholder="请输入标题名称" style="width:900px">
              <el-button slot="append" icon="el-icon-search">查重</el-button>
            </el-input>
          </el-form-item>
          <!-- ! 第二行 -->
          <el-row type="flex">
            <el-col :span="10">
              <el-form-item label="作者" prop="author">
                <el-input v-model="form.author" placeholder="请输入作者" style="width:430px" />
              </el-form-item>
            </el-col>
            <el-col :span="14">
              <el-form-item label="分类" prop="cateid">
                <el-select v-model="form.cateid" placeholder="请选择">
                  <el-option
                    v-for="item in options"
                    :key="item.id"
                    :label="item.catename"
                    :value="item.id"
                    style="width:220px"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- ! 第三行 -->
          <el-row type="flex">
            <el-col :span="10">
              <el-form-item label="阅读次数">
                <el-input-number v-model="form.click" :min="1" :max="10000" label="阅读次数" />
              </el-form-item>
            </el-col>
            <el-col :span="14">
              <el-form-item label="创建时间" prop="create_date">
                <el-date-picker
                  v-model="form.create_date"
                  type="datetime"
                  placeholder="选择日期时间"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- !第四行 -->
          <el-row>
            <el-col :span="10">
              <el-form-item label="是否推荐">
                <el-switch
                  v-model="form.ishot"
                  :active-value="1"
                  :inactive-value="0"
                  active-text="是"
                  inactive-text="否"
                />
              </el-form-item>
            </el-col>
            <el-col :span="14">
              <el-form-item label="是否顶置">
                <el-switch
                  v-model="form.istop"
                  :active-value="1"
                  :inactive-value="0"
                  active-text="是"
                  inactive-text="否"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- ! 第五行 -->
          <el-form-item label="收藏数量" prop="lovenum">
            <el-input v-model="form.lovenum" placeholder="请输入收藏数量名称" style="width:440px" />
          </el-form-item>
          <!-- ! 第六行 -->
          <el-form-item label="文章缩略图" prop="pic">
            <el-upload
              ref="upload"
              v-model="form.pic"
              class="upload-demo"
              style="width:900px"
              action="http://124.223.14.236:8060/"
              :on-success="success"
              :file-list="fileList"
              list-type="picture"
            >
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
          </el-form-item>
          <!-- ! 第七行 -->
          <el-row>
            <el-col>
              <el-form-item label="文章标签" prop="tags">
                <el-checkbox-group v-model="tagsList">
                  <el-checkbox v-for="item in checkData" :key="item.id" :label="item.tagname">{{ item.tagname }}</el-checkbox>
                </el-checkbox-group>

              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="SEO信息" name="second">

          <el-form-item label="seo关键字" prop="keywords">
            <el-input v-model="form.keywords" type="textarea" placeholder="请输入seo关键字名称" rows="3" style="width:1090px" />
          </el-form-item>
          <el-form-item label="seo的描述" prop="description">
            <el-input v-model="form.description" type="textarea" placeholder="请输入seo描述" rows="3" style="width:1090px" />
          </el-form-item>
        </el-tab-pane>

        <el-tab-pane label="文章内容" name="third">
          <el-form-item label="文章内容" style="width:1200px" prop="content">
            <quill-editor v-model="form.content" @blur="$refs.formRef.validateField('content')" />
          </el-form-item>
        </el-tab-pane>
        <el-row type="flex" justify="center">
          <el-button type="success" icon="el-icon-plus" @click="release('2')">立即发布</el-button>
          <el-button type="primary" icon="el-icon-folder-checked">保存草稿</el-button>
          <el-button type="warning" icon="el-icon-back" @click="$router.push('/content_article')">返回列表</el-button>
        </el-row>
      </el-tabs>
    </el-form>
  </div>
</template>
<script>

// ! 1.下包 yarn add vue-quill-editor
// ! 2. 导入富文本编辑器样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

// ! 3.导入富文本编辑器的包
import { quillEditor } from 'vue-quill-editor'
import { getImg } from '@/api/adv/img'
import { getCateList, getTagList, addArticle, getArticleDetail, updateArticle } from '@/api/article/article'
export default {
  components: {
    // ! 4.注册
    quillEditor
  },
  data() {
    return {
      activeName: 'first',
      // 下拉列表数据
      options: [],
      // 复选框数据
      checkData: [],
      tagsList: [],
      // 上传
      fileList: [],
      form: {
        author: '',
        cateid: '',
        click: '',
        content: '',
        create_date: '',
        description: '',
        ishot: 0,
        istop: 0,
        keywords: '',
        lovenum: '',
        pic: '',
        status: '',
        tags: [],
        title: ''
      },
      rules: {
        author: [
          { required: true, message: '必须', trigger: 'blur' }
        ],
        cateid: [
          { required: true, message: '必须', trigger: 'change' }
        ],
        title: [
          { required: true, message: '必须', trigger: 'blur' }
        ],
        lovenum: [
          { required: true, message: '必须', trigger: 'blur' }
        ],
        create_date: [
          { required: true, message: '必须', trigger: 'change' }
        ],
        // tags: [
        //   { required: true, message: '必须', trigger: 'blur' }
        // ],
        keywords: [
          { required: true, message: '必须', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '必须', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '必须', trigger: 'blur' }
        ],
        pic: [
          { required: true, message: '必须', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (this.fileList) {
                callback()
              } else {
                callback(new Error('必须'))
              }
            },
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.getCateList()
    this.gettagList()

    this.$route.params.id && this.getArticleDetail()
  },
  methods: {
    // ! 获取文章详情
    async getArticleDetail() {
      const res = await getArticleDetail(this.$route.params.id)
      console.log(res)
      this.form = res
      // console.log(res.tags)

      this.tagsList = res.tags.split(',')
      console.log(this.tagsList)
      // ! 图片回显
      const obj = { name: '点击查看详情', url: 'http://124.223.14.236:8060/' + res.pic }
      if (res.pic === null) {
        this.fileList = []
      } else {
        this.fileList.push(obj)
      }
    },
    // !上传头像
    async success(response, files, fileList) {
      const file = fileList[0].raw
      console.log(file)

      const fd = new FormData()

      fd.append('file', file)
      const res = await getImg(fd)
      this.form.pic = res.savePath
      console.log(this.form.pic)
    },
    // ! 发布
    release(status) {
      this.$refs.formRef.validate(async isOk => {
        if (!isOk) return
        if (this.form.id) {
          this.form.tags = this.tagsList
          await updateArticle(this.form)
          this.$message.success('编辑成功')
        } else {
          this.form.status = status
          this.form.create_date = +new Date(this.form.create_date)
          await addArticle(this.form)

          this.$message.success('添加成功')
        }
        this.$router.push('/content_article')
      })
    },
    // ! 获取分类列表
    async getCateList() {
      const res = await getCateList()
      this.options = res
    },
    // ! 获取复选框内容
    async gettagList() {
      const res = await getTagList()
      this.checkData = res
      console.log(res)
    },
    handleClick() {

    }
  }
}
</script>
<style lang="scss" scope>
  .content{
    margin: 20px;
    padding: 20px;
    background-color: #fff;
    .el-tab-pane{
      padding-top: 50px;
      padding-left: 150px;
    }
  }
  .quill-editor {
  .ql-editor {
    height: 240px;
  }

}
</style>
